package com.datacow.modules.datamanagement.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.datacow.modules.datamanagement.entity.DatacowProductHuntEnEntity;
import com.datacow.modules.datamanagement.service.DatacowProductHuntEnService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.datacow.common.utils.PageUtils;
import com.datacow.common.utils.R;


/**
 * 英文格式爬取的数据表
 *
 * @author 李德森
 * copyright (c) 2021 数据牛( www.datacow.com )Inc. All rights reserved.
 * @date 2021-01-19 11:55:04
 */
@RestController
public class DatacowProductHuntEnController {
    @Autowired
    private DatacowProductHuntEnService datacowProductHuntEnService;

    /**
     * 列表
     */
    @RequestMapping("/producthunt/list")
    @RequiresPermissions("datamanagement:datacowproducthunten:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = datacowProductHuntEnService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/producthunt/info/{id}")
    @RequiresPermissions("api:producthunt:info")
    public R info(@PathVariable("id") Long id) {
        DatacowProductHuntEnEntity datacowProductHuntEn = datacowProductHuntEnService.getById(id);

        return R.ok().put("datacowProductHuntEn", datacowProductHuntEn);
    }

    /**
     * 保存
     */
    @RequestMapping("/producthunt/save")
    @RequiresPermissions("api:producthunt:save")
    public R save(@RequestBody DatacowProductHuntEnEntity datacowProductHuntEn) {
        datacowProductHuntEn.setUpdateTime(new Date());
        datacowProductHuntEn.setCreateTime(new Date());

        //TODO 要对图片进行处理
        datacowProductHuntEnService.save(datacowProductHuntEn);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/producthunt/update")
    @RequiresPermissions("api:producthunt:update")
    public R update(@RequestBody DatacowProductHuntEnEntity datacowProductHuntEn) {
        datacowProductHuntEn.setUpdateTime(new Date());
        datacowProductHuntEnService.updateById(datacowProductHuntEn);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/producthunt/delete")
    @RequiresPermissions("api:producthunt:delete")
    public R delete(@RequestBody Long[] ids) {
        datacowProductHuntEnService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 百度翻译接口
     */
    @RequestMapping(value = "/producthunt/translation", method = RequestMethod.GET)
    @RequiresPermissions("api:producthunt:translation")
    public R delete(@PathVariable("translationString") String translationString) throws Exception {
        String getTranslationData = datacowProductHuntEnService.translationEnCN(translationString);
        return R.ok().put("translationString", getTranslationData);
    }
}
